#include "x_config.h"
#include "json.h"

using namespace std;

int CXConfig::m_ssPort = 6000;
int CXConfig::m_httpPort = 8000;
int CXConfig::m_mcPort = 6004;
char CXConfig::m_mcAddr[16];
int CXConfig::m_ssId = 0;
char CXConfig::m_httpUrl[64];
char CXConfig::m_rootPasswd[32];
char CXConfig::m_configType[32];

CXConfig::CXConfig()
{
	memset(CXConfig::m_mcAddr, 0, sizeof(CXConfig::m_mcAddr));
	memset(CXConfig::m_httpUrl, 0, sizeof(CXConfig::m_httpUrl));
	memset(CXConfig::m_rootPasswd, 0, sizeof(CXConfig::m_rootPasswd));
	memset(CXConfig::m_configType, 0, sizeof(CXConfig::m_configType));
}

CXConfig::~CXConfig()
{

}

int CXConfig::Init()
{
	fstream fin;
	fin.open("x_conf.json", ifstream::in);

	ostringstream sout_temp;
	sout_temp << fin.rdbuf();
	string strConf(sout_temp.str());

	json_object *helper = json_object_new_object();
	helper = json_tokener_parse((char *)strConf.c_str());
	if(is_error(helper))
	{
		return -1;
	}
	m_ssPort = json_object_get_int(json_object_object_get(json_object_object_get(helper,(char *)"SreamServer"), (char *)"ss_port"));
	m_ssId = json_object_get_int(json_object_object_get(json_object_object_get(helper,(char *)"SreamServer"), (char *)"ss_id"));
	m_httpPort = json_object_get_int(json_object_object_get(json_object_object_get(helper,(char *)"HttpServer"), (char *)"http_port"));
	memcpy(CXConfig::m_mcAddr,
			json_object_get_string(
					json_object_object_get(
							json_object_object_get(helper,(char *)"MCServer"), (char *)"mc_addr")),
			strlen(json_object_get_string(
					json_object_object_get(
							json_object_object_get(helper,(char *)"MCServer"), (char *)"mc_addr"))));
	m_mcPort = json_object_get_int(json_object_object_get(json_object_object_get(helper,(char *)"MCServer"), (char *)"mc_port"));
	sprintf(CXConfig::m_httpUrl, "http://%s:%d/controller/request", CXConfig::m_mcAddr, m_mcPort);
	memcpy(CXConfig::m_rootPasswd,
			json_object_get_string(
					json_object_object_get(
							json_object_object_get(helper,(char *)"LocalConf"), (char *)"passwd")),
			strlen(json_object_get_string(
					json_object_object_get(
							json_object_object_get(helper,(char *)"LocalConf"), (char *)"passwd"))));
	memcpy(CXConfig::m_configType,
				json_object_get_string(
						json_object_object_get(
								json_object_object_get(helper,(char *)"LocalConf"), (char *)"conf_type")),
				strlen(json_object_get_string(
						json_object_object_get(
								json_object_object_get(helper,(char *)"LocalConf"), (char *)"conf_type"))));

	return 0;
}
